<?php

use yii\base\Model;
use yii\base\Security;
use yii\caching\CacheInterface;
use yii\db\Connection;
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\web\Application;
use yii\web\CookieCollection;
use yii\web\Request;
use yii\web\Response;
use yii\web\Session;
use yii\web\User;
use yii\web\View;

/**
 * 获取配置项
 * params
 * @param null $key
 * @param null $defaultValue
 * @return mixed|null
 */
function params($key = null, $defaultValue = null)
{
    $params = app()->params;

    return $params[$key] ?? $defaultValue;
}

/**
 * app
 * @return \yii\console\Application|Application
 */
function app()
{
    return Yii::$app;
}

/**
 * user
 * @return mixed|User
 */
function user()
{
    return app()->user;
}

/**
 * userInfo
 * @param null $attribute
 * @return null
 */
function userInfo($attribute = null)
{
    $userInfo = user()->identity;
    return $userInfo->$attribute??null;
}

/**
 * request
 * @return \yii\console\Request|Request
 */
function request()
{
    return app()->request;
}

/**
 * get
 * @param null $name
 * @param null $defaultValue
 * @return array|mixed
 */
function get($name = null, $defaultValue = null)
{
    return request()->get($name, $defaultValue);
}

/**
 * post
 * @param null $name
 * @param null $defaultValue
 * @return array|mixed
 */
function post($name = null, $defaultValue = null)
{
    return request()->post($name, $defaultValue);
}

/**
 * response
 * @return \yii\console\Response|Response
 */
function response()
{
    return app()->response;
}

/**
 * view
 * @return \yii\base\View|View
 */
function view()
{
    return app()->view;
}

/**
 * db
 * @return Connection
 */
function db()
{
    return app()->db;
}

/**
 * cache
 * @return CacheInterface
 */
function cache()
{
    return app()->cache;
}

/**
 * @return mixed|Session
 */
function session()
{
    return app()->session;
}

/**
 * requestCookies
 * @return CookieCollection
 */
function requestCookies()
{
    return request()->cookies;
}

/**
 * responseCookies
 * @return CookieCollection
 */
function responseCookies()
{
    return response()->cookies;
}

/**
 * security
 * @return Security
 */
function security()
{
    return app()->security;
}

/**
 * boolTexts
 * @param $value
 * @param string $label
 * @return string
 */
function boolTexts($value,$label=''){
    return Html::label($label,null,[
        'class' => 'label label-'.($value?'success':'danger')
    ]);
}

/**
 * exceptionFormat
 * @param Exception $exception
 * @param bool $log
 * @param string $category
 * @return array
 */
function exceptionFormat($exception,$log=true,$category='application')
{
    $exceptionArray = [
        'code'        => $exception->getCode(),
        'file'        => $exception->getFile(),
        'line'        => $exception->getLine(),
        'message'     => $exception->getMessage(),
        'traceString' => $exception->getTraceAsString(),
        // 'trace'       => $exception->getTrace(),
    ];

    $log && Yii::error($exceptionArray,$category);

    if(!$log)
        return $exceptionArray;
}

/**
 * getModelErrorMsg
 * @param Model $model
 * @param string $defaultMsg
 * @return string|null
 */
function getModelErrorMsg($model,$defaultMsg='操作失败')
{
    if ($model->hasErrors()) {
        $mags = array_values($model->getFirstErrors());
        return current($mags);
    }
    return $defaultMsg;
}


/**
 * 16位唯一id
 * createUniqueId
 * @return string
 */
function createUniqueId()
{
    list($t1, $t2) = explode(' ', microtime());
    //秒+毫秒
    return date('ymdHi').substr($t1,2,6);
}

/**
 * isMobile
 * @param $mobile
 * @return false|int
 */
function isMobile($mobile)
{
    return preg_match("/^1[3456789]{1}\d{9}$/", $mobile);
}

/**
 * 当前时间
 * dateNow
 * @return false|string
 */
function dateNow()
{
    return date('Y-m-d H:i:s');
}

/**
 * 昨天凌晨时间戳
 * yesterdayTimestamp
 * @return false|int
 */
function yesterdayTimestamp()
{
    return strtotime('yesterday');
}

/**
 * 今天凌晨时间戳
 * todayTimestamp
 * @return false|int
 */
function todayTimestamp()
{
    return strtotime(date('Y-m-d'));
}

/**
 * 昨天凌晨时间
 * yesterdayDateTime
 * @return false|string
 */
function yesterdayDateTime()
{
    return date('Y-m-d H:i:s',yesterdayTimestamp());
}

/**
 * 今天凌晨时间
 * todayDateTime
 * @return false|string
 */
function todayDateTime()
{
    return date('Y-m-d H:i:s',todayTimestamp());
}

/**
 * 时间戳转年月日
 * timeToDate
 * @param $value
 * @return false|string
 */
function timeToDate($value)
{
    return date('Y-m-d',$value);
}